<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="任务名称" prop="taskName">
				<el-input v-model="form.taskName" placeholder="任务名称" maxlength="48" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="执行类" prop="method">
				<el-input v-model="form.method" placeholder="计划任务执行类名称" clearable maxlength="280"></el-input>
			</el-form-item>
			<el-form-item label="定时规则" prop="cron">
				<sc-cron v-model="form.cron" placeholder="请输入Cron定时规则" clearable :shortcuts="shortcuts" maxlength="48"></sc-cron>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="0" inactive-value="1"></el-switch>
			</el-form-item>
			<el-form-item label="任务说明">
				<el-input type="textarea" :rows="2" maxlength="180" placeholder="任务说明" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import scCron from '@/components/scCron';

export default {
	components: {
		scCron
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增计划任务',
				edit: '编辑计划任务'
			},
			form: {
				taskId: "",
				taskName: "",
				sortNo:1,
				method: "",
				remark: "",
				cron: "",
				status: "1"
			},
			rules: {
				taskName: [
					{required: true, message: '请填写标题'}
				],
				method: [
					{required: true, message: '请填写执行类'}
				],
				cron: [
					{required: true, message: '请填写定时规则'}
				],
				sortNo: [
					{required: true, message: '请输入排序', trigger: 'change'}
				],
			},
			visible: false,
			isSaveing: false,
			shortcuts: [
				{
					text: "每天8点和12点 (自定义追加)",
					value: "0 0 8,12 * * ?"
				}
			]
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if(this.mode=="add")
					{
						this.isSaveing = true;
						res = await this.$API.system.sysTasks.insert.post(this.form);
						this.isSaveing = false;
					}else if(this.mode=="edit")
					{
						this.isSaveing = true;
						res = await this.$API.system.sysTasks.update.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.taskId = data.taskId
			this.form.taskName = data.taskName
			this.form.sortNo = data.sortNo
			this.form.method = data.method
			this.form.cron = data.cron
			this.form.remark = data.remark
			this.form.status = data.status
		}
	}
}
</script>

<style>
</style>
